# Copyright (c) 2024 anqi.huang@outlook.com
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use self file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

cmake_minimum_required(VERSION 3.5)

project(libmediadrmmetrics_full)

# Check if the major CMakeLists.txt file has correctly set
# the ANDROID_ROOT directory.
if (IS_DIRECTORY ${ANDROID_ROOT})
    message(STATUS "${PROJECT_NAME}, has been set android root")
else ()
    if (IS_DIRECTORY ${BUILD_NATIVE_ROOT})
        message(WARNING "${PROJECT_NAME}, set ANDROID_ROOT via BUILD_NATIVE_ROOT=${BUILD_NATIVE_ROOT}")
        set(ANDROID_ROOT ${BUILD_NATIVE_ROOT})
        set(OUT_INTERMEDIATES_ROOT ${ANDROID_ROOT}/out/soong/.intermediates)
    else ()
        message(FATAL_ERROR "${PROJECT_NAME}, ANDROID_ROOT was NULL")
    endif ()
endif ()

#################### libmediadrmmetrics_full ####################
set(FILES
        ${ANDROID_ROOT}/frameworks/av/drm/libmediadrm/DrmMetrics.cpp
        ${ANDROID_ROOT}/frameworks/av/drm/libmediadrm/PluginMetricsReporting.cpp
        ${OUT_INTERMEDIATES_ROOT}/frameworks/av/drm/libmediadrm/libmediadrmmetrics_full/android_${OUT_ARCH_CPU}_shared/gen/proto/frameworks/av/drm/libmediadrm/protos/metrics.pb.cc
)
set(SOURCE_FILES "")
foreach (file ${FILES})
    if (EXISTS ${file})
        list(APPEND SOURCE_FILES ${file})
    endif ()
endforeach ()

if (NOT SOURCE_FILES)
    message(WARNING "${PROJECT_NAME}, source files not exist")
else ()
    add_library(libmediadrmmetrics_full SHARED ${SOURCE_FILES})

    target_include_directories(libmediadrmmetrics_full
            PUBLIC
            ${ANDROID_ROOT}/frameworks/av/drm/libmediadrm/include
            ${ANDROID_ROOT}/frameworks/av/drm/libmediadrm
            ${OUT_INTERMEDIATES_ROOT}/frameworks/av/drm/libmediadrm/libmediadrmmetrics_full/android_${OUT_ARCH_CPU}_shared/gen/proto/frameworks/av/drm/libmediadrm
            ${OUT_INTERMEDIATES_ROOT}/frameworks/av/drm/libmediadrm/libmediadrmmetrics_full/android_${OUT_ARCH_CPU}_shared/gen/proto
    )

    # Add c/c++ flags
    target_compile_options(libmediadrmmetrics_full PRIVATE
            -Werror
            -Wno-unused-parameter
    )

    target_link_libraries(libmediadrmmetrics_full
            android.hardware.drm-1.0
            android.hardware.drm-1.1
            android.hardware.drm-1.2
            libbase
            libc
            libc++
            libc++demangle
            libclang_rt.builtins
            libdl
            liblog
            libm
            libmedia_headers
            libmediametrics
            libprotobuf-cpp-full
            libstagefright_foundation
            libutils
    )
endif ()
#################### libmediadrmmetrics_full ####################

#################### libmediadrm ####################
set(FILES
        ${ANDROID_ROOT}/frameworks/av/drm/libmediadrm/DrmPluginPath.cpp
        ${ANDROID_ROOT}/frameworks/av/drm/libmediadrm/DrmSessionManager.cpp
        ${ANDROID_ROOT}/frameworks/av/drm/libmediadrm/SharedLibrary.cpp
        ${ANDROID_ROOT}/frameworks/av/drm/libmediadrm/DrmHal.cpp
        ${ANDROID_ROOT}/frameworks/av/drm/libmediadrm/DrmHalHidl.cpp
        ${ANDROID_ROOT}/frameworks/av/drm/libmediadrm/DrmHalAidl.cpp
        ${ANDROID_ROOT}/frameworks/av/drm/libmediadrm/CryptoHal.cpp
        ${ANDROID_ROOT}/frameworks/av/drm/libmediadrm/CryptoHalHidl.cpp
        ${ANDROID_ROOT}/frameworks/av/drm/libmediadrm/CryptoHalAidl.cpp
        ${ANDROID_ROOT}/frameworks/av/drm/libmediadrm/DrmUtils.cpp
        ${ANDROID_ROOT}/frameworks/av/drm/libmediadrm/DrmHalListener.cpp
        ${ANDROID_ROOT}/frameworks/av/drm/libmediadrm/DrmStatus.cpp
        ${ANDROID_ROOT}/frameworks/av/drm/libmediadrm/DrmMetricsLogger.cpp
)
set(SOURCE_FILES "")
foreach (file ${FILES})
    if (EXISTS ${file})
        list(APPEND SOURCE_FILES ${file})
    endif ()
endforeach ()

if (NOT SOURCE_FILES)
    message(WARNING "${PROJECT_NAME}, source files not exist")
else ()
    add_library(libmediadrm SHARED ${SOURCE_FILES})

    target_include_directories(libmediadrm
            PUBLIC
            ${ANDROID_ROOT}/frameworks/av/drm/libmediadrm/include
            ${ANDROID_ROOT}/frameworks/av/drm/libmediadrm/interface
            ${ANDROID_ROOT}/frameworks/av/drm/libmediadrm
            ${OUT_INTERMEDIATES_ROOT}/frameworks/av/drm/libmediadrm/libmediadrmmetrics_lite/android_${OUT_ARCH_CPU}_shared/gen/proto/frameworks/av/drm/libmediadrm
            ${OUT_INTERMEDIATES_ROOT}/frameworks/av/drm/libmediadrm/libmediadrmmetrics_lite/android_${OUT_ARCH_CPU}_shared/gen/proto
    )

    # Add c/c++ flags
    target_compile_options(libmediadrm PRIVATE
            -Werror
            -Wall
    )

    target_link_libraries(libmediadrm
            android.hardware.drm-V1-ndk
            android.hardware.drm-1.0
            android.hardware.drm-1.1
            android.hardware.drm-1.2
            android.hardware.drm-1.3
            android.hardware.drm-1.4
            libaidlcommonsupport
            libbinder_ndk
            libc
            libc++
            libc++_static
            libc++demangle
            libclang_rt.builtins
            libclang_rt.ubsan_minimal
            libcutils
            libdl
            libhidlallocatorutils
            libhidlbase
            libjsoncpp
            liblog
            libm
            libmedia
            libmedia_headers
            libmediadrmmetrics_lite
            libmediametrics
            libmediautils
            libstagefright_foundation
            libutils
            resourcemanager_aidl_interface-ndk
    )
endif ()
#################### libmediadrm ####################

#################### libmediadrmmetrics_lite ####################
set(FILES
        ${ANDROID_ROOT}/frameworks/av/drm/libmediadrm/DrmMetrics.cpp
        ${ANDROID_ROOT}/frameworks/av/drm/libmediadrm/PluginMetricsReporting.cpp
        ${OUT_INTERMEDIATES_ROOT}/frameworks/av/drm/libmediadrm/libmediadrmmetrics_lite/android_${OUT_ARCH_CPU}_shared/gen/proto/frameworks/av/drm/libmediadrm/protos/metrics.pb.cc
)
set(SOURCE_FILES "")
foreach (file ${FILES})
    if (EXISTS ${file})
        list(APPEND SOURCE_FILES ${file})
    endif ()
endforeach ()

if (NOT SOURCE_FILES)
    message(WARNING "${PROJECT_NAME}, source files not exist")
else ()
    add_library(libmediadrmmetrics_lite SHARED ${SOURCE_FILES})

    target_include_directories(libmediadrmmetrics_lite
            PUBLIC
            ${ANDROID_ROOT}/frameworks/av/drm/libmediadrm/include
            ${ANDROID_ROOT}/frameworks/av/drm/libmediadrm
            ${OUT_INTERMEDIATES_ROOT}/frameworks/av/drm/libmediadrm/libmediadrmmetrics_lite/android_${OUT_ARCH_CPU}_shared/gen/proto/frameworks/av/drm/libmediadrm
            ${OUT_INTERMEDIATES_ROOT}/frameworks/av/drm/libmediadrm/libmediadrmmetrics_lite/android_${OUT_ARCH_CPU}_shared/gen/proto
    )

    # Add c/c++ flags
    target_compile_options(libmediadrmmetrics_lite PRIVATE
            -Werror
            -Wno-unused-parameter
    )

    target_link_libraries(libmediadrmmetrics_lite
            android.hardware.drm-1.0
            android.hardware.drm-1.1
            android.hardware.drm-1.2
            libc
            libc++
            libc++demangle
            libclang_rt.builtins
            libdl
            liblog
            libm
            libmedia_headers
            libmediametrics
            libprotobuf-cpp-lite
            libutils
    )
endif ()
#################### libmediadrmmetrics_lite ####################

#################### libmediadrmmetrics_consumer ####################
set(FILES
        ${ANDROID_ROOT}/frameworks/av/drm/libmediadrm/DrmMetricsConsumer.cpp
)
set(SOURCE_FILES "")
foreach (file ${FILES})
    if (EXISTS ${file})
        list(APPEND SOURCE_FILES ${file})
    endif ()
endforeach ()

if (NOT SOURCE_FILES)
    message(WARNING "${PROJECT_NAME}, source files not exist")
else ()
    add_library(libmediadrmmetrics_consumer SHARED ${SOURCE_FILES})

    target_include_directories(libmediadrmmetrics_consumer
            PUBLIC
            ${ANDROID_ROOT}/frameworks/av/drm/libmediadrm
            ${ANDROID_ROOT}/frameworks/av/drm/libmediadrm/include
            ${OUT_INTERMEDIATES_ROOT}/frameworks/av/drm/libmediadrm/libmediadrmmetrics_full/android_${OUT_ARCH_CPU}_shared/gen/proto/frameworks/av/drm/libmediadrm
            ${OUT_INTERMEDIATES_ROOT}/frameworks/av/drm/libmediadrm/libmediadrmmetrics_full/android_${OUT_ARCH_CPU}_shared/gen/proto
    )

    # Add c/c++ flags
    target_compile_options(libmediadrmmetrics_consumer PRIVATE
            -Werror
    )

    target_link_libraries(libmediadrmmetrics_consumer
            android.hardware.drm-1.0
            android.hardware.drm-1.1
            android.hardware.drm-1.2
            libbinder
            libc
            libc++
            libc++demangle
            libclang_rt.builtins
            libdl
            libhidlbase
            liblog
            libm
            libmedia_headers
            libmediadrm
            libmediadrmmetrics_full
            libmediametrics_headers
            libstagefright_foundation_headers
            libutils
    )
endif ()
#################### libmediadrmmetrics_consumer ####################
